package com.bsh.serviceImpl;



import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.bsh.api.ApiUtils;
import com.bsh.api.Exception.ApiException;
import com.bsh.service.WxPayService;
import com.bsh.utils.ApiMessage;
import com.bsh.utils.Constants;
@Service
public class WxPayServiceImpl implements WxPayService {

	@Override
	public ApiMessage getParam(JSONObject dto) throws ApiException {
		ApiMessage msg = new ApiMessage();
		ApiUtils.checkUserNameToken(dto);
		if(ApiUtils.objIsEmpty((dto.getInteger("notify_url")))){
			throw new ApiException("400","回调地址不能为空");
		}
		if(ApiUtils.objIsEmpty((dto.getInteger("sn")))){
			throw new ApiException("400","订单号不能为空");
		}
		if(ApiUtils.objIsEmpty((dto.getInteger("total_fee")))){
			throw new ApiException("400","交易金额不能为空");
		}
		if(ApiUtils.objIsEmpty((dto.getInteger("openid")))){
			throw new ApiException("400","openid不能为空");
		}
		JSONObject json=new JSONObject();
		String nonceStr=ApiUtils.uuid();
	    json.put("appid",Constants.WX.APP_ID);
	    json.put("mch_id",Constants.WX.MCH_ID );
	    json.put("device_info",Constants.WX.DEVICE_INFO);
	    json.put("body",Constants.WX.BODY );
	    json.put("trade_type",Constants.WX.TRADE_TYPE);
	    json.put("nonce_str",nonceStr);
	    json.put("sign", ApiUtils.createWxSign(nonceStr));
	    json.put("notify_url",dto.getInteger("notify_url"));
	    json.put("out_trade_no",dto.getInteger("sn"));
	    json.put("total_fee",dto.getInteger("total_fee"));
	    json.put("openid",dto.getInteger("openid"));
        
        //构造xml	
	    Document document = DocumentHelper.createDocument();
	    Element root = document.addElement("xml");
	    Element appid = root.addElement("appid");
	    appid.addText(json.getString("appid"));
	    Element body = root.addElement("body");
	    body.addText(json.getString("body"));
	    Element device_info = root.addElement("device_info");
	    device_info.addText(json.getString("device_info"));
	    Element mch_id = root.addElement("mch_id");
	    mch_id.addText(json.getString("mch_id"));
	    Element nonce_str = root.addElement("nonce_str");
	    nonce_str.addText(json.getString("nonce_str"));
	    Element notify_url = root.addElement("notify_url");
	    notify_url.addText(json.getString("notify_url"));
	    Element openid = root.addElement("openid");
	    openid.addText(json.getString("openid"));
	    Element out_trade_no = root.addElement("out_trade_no");
	    out_trade_no.addText(json.getString("out_trade_no"));
	    Element sign = root.addElement("sign");
	    sign.addText(json.getString("sign"));
	    Element total_fee = root.addElement("total_fee");
	    total_fee.addText(json.getString("total_fee"));
	    Element trade_type = root.addElement("trade_type");
	    trade_type.addText(json.getString("trade_type"));
        
	    
	    return msg;
		
	}

}
